<?php

namespace app\data\controller;

use cmf\controller\AdminBaseController;

class DataController extends AdminBaseController
{
    /**
     * 职位列表
     * @adminMenu123123(
     *     'name'   => '职位列表',
     *     'parent' => 'department/department/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '职位列表',
     *     'param'  => ''
     * )
     */
    function index()
    {
//        header("Content-type:text/html;charset=utf-8");
//
//        $this->getdata();
        $where_r = input();
        $this->assign('where_r', $where_r);
        return $this->fetch();
    }

    private function getdata($groups = null, $starttime = 0, $endtime = 0)
    {
        $where = [];
        $where_c = [];
        if ($groups) {
            $where = [
                'department.id' => [
                    'in', $groups
                ]
            ];
            $where_c = [
                'department.id' => [
                    'in', $groups
                ]
            ];
        }

        if ($starttime) {
            $where['createtime'] = ['egt', $starttime];
            $where_c['lists.create_time'] = ['egt', $starttime];
        }
        if (input('end_time')) {
            $datetime = strtotime(date('Y-m-d', $endtime));
            if ($endtime == $datetime) {
                $endtime += 86399;
            }
            if (isset($where['createtime'])) {
                $where['createtime'] = ['between', [$starttime, $endtime]];
                $where_c['lists.create_time'] = ['between', [$starttime, $endtime]];
            } else {
                $where['createtime'] = ['elt', $endtime];
                $where_c['lists.create_time'] = ['elt', $endtime];
            }
        }

        $data = [];

        //卖家数据
        $data['license'] = db('license_plate')
            ->alias('lists')
            ->join('__LICENSE_PLATE_USER__ lpuser', 'lists.id = lpuser.pid and lpuser.level = 1')
            ->join('__DEPARTMENT_USER__ depuser', 'lpuser.uid = depuser.user_id', 'left')
            ->join('__DEPARTMENT__ department', 'department.id = depuser.dep_id', 'left')
            ->field([
                'count(lists.id) as lcount'
            ])
            ->where($where)
//            ->group('dep_id')
            ->find();

        //买家数据
        $data['buyer'] = db('buyer')
            ->alias('lists')
            ->join('__BUYER_USER__ buser', 'lists.id = buser.pid and buser.level = 1')
            ->join('__DEPARTMENT_USER__ depuser', 'buser.uid = depuser.user_id', 'left')
            ->join('__DEPARTMENT__ department', 'department.id = depuser.dep_id', 'left')
            ->field([
                'count(lists.id) as lcount'
            ])
            ->where($where)
//            ->group('depuser.dep_id')
            ->find();

        //售电数据
        $data['shoudian'] = db('shoudian')
            ->alias('lists')
            ->join('__DEPARTMENT_USER__ depuser', 'lists.uid = depuser.user_id', 'left')
            ->join('__DEPARTMENT__ department', 'department.id = depuser.dep_id', 'left')
            ->field([
                'count(lists.id) as lcount'
            ])
            ->where($where_c)
//            ->group('depuser.dep_id')
            ->find();

        //中介数据
        $data['medium'] = db('medium')
            ->alias('lists')
            ->join('__DEPARTMENT_USER__ depuser', 'lists.uid = depuser.user_id', 'left')
            ->join('__DEPARTMENT__ department', 'department.id = depuser.dep_id', 'left')
            ->field([
                'count(lists.id) as lcount'
            ])
            ->where($where_c)
//            ->group('depuser.dep_id')
            ->find();

        //跟进数据
        $data['follow'] = db('follow')
            ->alias('lists')
            ->join('__DEPARTMENT_USER__ depuser', 'lists.uid = depuser.user_id', 'left')
            ->join('__DEPARTMENT__ department', 'department.id = depuser.dep_id', 'left')
            ->field([
                'count(lists.id) as lcount'
            ])
            ->where($where_c)
//            ->group('depuser.dep_id')
            ->find();

        //虚拟地址
        $data['virtual_address'] = db('virtual_address')
            ->alias('lists')
            ->join('__DEPARTMENT_USER__ depuser', 'lists.uid = depuser.user_id', 'left')
            ->join('__DEPARTMENT__ department', 'department.id = depuser.dep_id', 'left')
            ->field([
                'count(lists.id) as lcount'
            ])
            ->where($where_c)
//            ->group('depuser.dep_id')
            ->find();

        $where_1 = $where_c;
        if(isset($where_1['department.id'])) {
            unset($where_1['department.id']);
        }
        //潜在客户
        $data['potential_customer'] = db('potential_customer')
            ->alias('lists')
            ->field([
                'count(lists.id) as lcount'
            ])
            ->where($where_1)
//            ->group('depuser.dep_id')
            ->find();


        foreach ($data as $key => &$v) {
            switch ($key) {
                case 'license':
                    $v['data_title'] = '卖家数据';
                    break;
                case 'buyer':
                    $v['data_title'] = '买家数据';
                    break;
                case 'shoudian':
                    $v['data_title'] = '售电数据';
                    break;
                case 'medium':
                    $v['data_title'] = '中介数据';
                    break;
                case 'follow':
                    $v['data_title'] = '跟进数据';
                    break;
                case 'virtual_address':
                    $v['data_title'] = '虚拟地址';
                    break;
                case 'potential_customer':
                    $v['data_title'] = '潜在客户';
                    break;
            }
        }
        return $data;
    }

    private function format_data($data)
    {
//        $dep = db('department')->field('id,parent_id,name')->select()->toArray();
//        print_r($dep);die;
        print_r($data);
        return '';
    }

    function getdatatodep()
    {
        $parent_id = $this->request->post('parent_id', 1, 'intval');
        $starttime = $this->request->post('start_time', 0, 'strtotime');
        $endtime = $this->request->post('end_time', 0, 'strtotime');
        $title = db('department')->where([
            'id' => $parent_id
        ])->field('id,name')->find();
        $departments = db('department')->where([
            'parent_id' => $parent_id
        ])->field('id,name')->select()->toArray();
        $title['total'] = 0;
        foreach ($departments as &$department) {
            $groups = getGroups($department['id']);
            $data = $this->getdata($groups, $starttime, $endtime);
            $size = 0;
            foreach ($data as $val) {
                $size += $val['lcount'];
            }
            $department['size'] = $size;
            $title['total'] += $size;
        }
        $departments['title'] = $title;

        return $departments;
    }

    function getdatatotype()
    {
        $dep_id = $this->request->post('dep_id', 1, 'intval');
        $starttime = $this->request->post('start_time', 0, 'strtotime');
        $endtime = $this->request->post('end_time', 0, 'strtotime');
        $title = db('department')->where([
            'id' => $dep_id
        ])->field('id,name')->find();
        $groups = getGroups($title['id']);

        $data = $this->getdata($groups, $starttime, $endtime);
        $title['total'] = 0;
        foreach ($data as $val) {
            $title['total'] += $val['lcount'];;
        }
        $data['title'] = $title;

        return $data;
    }

    function baidu()
    {
        return $this->fetch();
    }

    function getdatatobaidu()
    {
        $lists = db('sites_info')
            ->alias('info')
            ->join('__SITES__ sites', 'sites.id = info.site_id')
            ->field('sites.id,sites.domain,count(sites.id) as scount')
            ->group('sites.id')
            ->select();
        $title['total'] = 0;
        foreach ($lists as $val) {
            $title['total'] += $val['scount'];;
        }
        $lists['title'] = $title;
        return $lists;
    }

    function cate()
    {
        $where_r = input();
        $this->assign('where_r', $where_r);
        return $this->fetch();
    }

    function getdatatocate()
    {
        $starttime = $this->request->post('start_time', 0, 'strtotime');
        $endtime = $this->request->post('end_time', 0, 'strtotime');

        $datas = $this->getdata(getGroups(1, false), $starttime, $endtime);
        $title['total'] = 0;
        $cate = [
            'buyer', 'license', 'follow'
        ];
        foreach ($datas as $key => &$data) {
            $title['total'] += $data['lcount'];
            if (in_array($key, $cate)) {
                $data['cate'] = true;
            }
        }
        $title['name'] = '总';
        $datas['title'] = $title;

        return $datas;
    }

    function getdatatoonecate()
    {
        $type = $this->request->post('type');
        $starttime = $this->request->post('start_time', 0, 'strtotime');
        $endtime = $this->request->post('end_time', 0, 'strtotime');

        $datas = $this->getvalue($type, $starttime, $endtime);

        $title['total'] = 0;
        foreach ($datas as $key => $data) {
            if($key === 'data_title') continue;
            $title['total'] += $data['lcount'];
        }
        $title['name'] = '总';
        $datas['title'] = $title;

        return $datas;
    }

    function getvalue($where = [], $starttime = 0, $endtime = 0)
    {
        $data = [];
//        if ($this->get_status($where, 'license')) {
//            $data['license'] = $this->get_license($where, $starttime, $endtime);
//        }
//        if ($this->get_status($where, 'buyer')) {
//            $data['buyer'] = $this->get_license($where, $starttime, $endtime);
//        }
        if ($where == 'license') {
            $data['license'] = $this->get_license($starttime, $endtime);
        }
        if ($where == 'buyer') {
            $data['buyer'] = $this->get_buyer($starttime, $endtime);
        }
        if ($where == 'follow') {
            $data['follow'] = $this->get_follow($starttime, $endtime);
        }
        foreach ($data as $key => &$v) {
            switch ($key) {
                case 'license':
                    $v['data_title'] = '卖家数据';
                    break;
                case 'buyer':
                    $v['data_title'] = '买家数据';
                    break;
                case 'shoudian':
                    $v['data_title'] = '售电数据';
                    break;
                case 'medium':
                    $v['data_title'] = '中介数据';
                    break;
                case 'follow':
                    $v['data_title'] = '跟进数据';
                    break;
                case 'virtual_address':
                    $v['data_title'] = '虚拟地址';
                    break;
            }
        }
        return isset($data[$where])?$data[$where]:[];
    }

    private function get_status($where, $type = '')
    {
        if (!$where || isset($where['all']) || isset($where[$type])) return true;
        return false;
    }

    private function get_license($starttime = 0, $endtime = 0)
    {
        $where = [];
        if ($starttime) {
            $where['lists.createtime'] = ['egt', $starttime];
        }
        if ($endtime) {
            $datetime = strtotime(date('Y-m-d', $endtime));
            if ($endtime == $datetime) {
                $endtime += 86399;
            }
            if (isset($where['lists.createtime'])) {
                $where['lists.createtime'] = ['between', [$starttime, $endtime]];
            } else {
                $where['lists.createtime'] = ['elt', $endtime];
            }
        }

        $lists = db('license_plate')
            ->alias('lists')
            ->join('__INDUSTRY_CATE__ cate','lists.cid = cate.id')
            ->field([
                'count(lists.id) as lcount,cate.name'
            ])
            ->where('manager != 76')
            ->where($where)
            ->group('lists.cid')
            ->select()->toArray();
        return $lists;
    }

    private function get_buyer($starttime = 0, $endtime = 0)
    {
        $where = [];
        if ($starttime) {
            $where['buyer.create_time'] = ['egt', $starttime];
        }
        if ($endtime) {
            $datetime = strtotime(date('Y-m-d', $endtime));
            if ($endtime == $datetime) {
                $endtime += 86399;
            }
            if (isset($where['buyer.create_time'])) {
                $where['buyer.create_time'] = ['between', [$starttime, $endtime]];
            } else {
                $where['buyer.create_time'] = ['elt', $endtime];
            }
        }

        //买家数据
        $lists = db('industry_type')
            ->alias('lists')
            ->join('__INDUSTRY_CATE__ cate','lists.cate_id = cate.id')
            ->join('__BUYER__ buyer','lists.pid = buyer.id')
            ->field([
                'count(lists.id) as lcount,cate.name'
            ])
            ->where($where)
            ->group('lists.cate_id')
            ->select()->toArray();
        return $lists;
    }

    private function get_follow($starttime = 0, $endtime = 0)
    {
        $where = [];
        if ($starttime) {
            $where['lists.create_time'] = ['egt', $starttime];
        }
        if ($endtime) {
            $datetime = strtotime(date('Y-m-d', $endtime));
            if ($endtime == $datetime) {
                $endtime += 86399;
            }
            if (isset($where['lists.create_time'])) {
                $where['lists.create_time'] = ['between', [$starttime, $endtime]];
            } else {
                $where['lists.create_time'] = ['elt', $endtime];
            }
        }

        $departments = db('department')->where([
            'parent_id' => 1
        ])->field('id,name')->select()->toArray();
        $title['total'] = 0;
        $result = [];
        foreach ($departments as &$department) {
            $groups = getGroups($department['id']);

            $where['department.id'] = [
                'in', $groups
            ];

            //跟进数据
            $data = db('follow')
                ->alias('lists')
                ->join('__DEPARTMENT_USER__ depuser', 'lists.uid = depuser.user_id', 'left')
                ->join('__DEPARTMENT__ department', 'department.id = depuser.dep_id', 'left')
                ->field([
                    'count(lists.id) as lcount'
                ])
                ->where($where)
//            ->group('depuser.dep_id')
                ->find();
            $data['name'] = $department['name'];

            $title['total'] += $data['lcount'];
            $result[] = $data;
        }

        return $result;
    }
}